
/**
 * 解析json 获取value
 */
export function getValueFromJson(json, key) {
    let object = JSON.parse(json);
    if(object == null){
        return null;
    }
    return object[key];
}

/**
 * @author:weixl
 * @description: {"label":"选项卡一","value":"68e447c0fbe84de694a072a99364cb57"}
 *                  通过key获取指定数据 [{"label":"","value":""},{"label":"选项卡一","value":"68e447c0fbe84de694a072a99364cb57"}]
 * @date: 2018/6/4 11:15
 * @params :
 * @return :
 */
export const getValueByKey = (key, data) => {
    let value = '';
    if(typeof data == 'boolean'){
        return data;
    }
    if (!data || !key) {
        return value
    }
    if (data instanceof Array) {
        let result = []
        for (let i in data) {
            if (data[i][key]) {
                result.push(data[i][key])
            }
        }
        return result
    } else if (typeof data == 'object') {
        if (data[key]) {
            value = data[key]
        }
    }
    if (value == '') {
        value = data
    }
    return value
}
/**
 * 转换数组中的对象属性，如：let a = [{id: 1, text: 2}]，调用trnasformPropery(a, {id: 'value', text: 'label'}) ====> //a = [{value: 1, label: 2}];
 * @param data
 * @param fieldNameMap
 * @returns {Array}
 */
export function transformProperty (data, fieldNameMap) {
    if (!data) {
        return []
    }
    let tmpData = JSON.parse(JSON.stringify(data))

    for (let i = 0; i < tmpData.length; i++) {
        for (let prop in fieldNameMap) {
            if (tmpData[i][prop]) {
                tmpData[i][fieldNameMap[prop]] = tmpData[i][prop]
                delete tmpData[i][prop]
            }
        }
    }

    return tmpData
}
/**
 * 修改data的属性
 * @param data
 * @param mapObj    如：{id: 'value'}意思为把data里的id属性全部改为value属性
 */
export const transformData = (data, mapObj) => {
    if (!data) {
        return []
    }
    let newData = []

    data.forEach(item => {
        let newItem = {...item}
        for (let i in mapObj) {
            let key = i
            let key1
            if (i.indexOf(',') != -1) {
                key = i.split(',')[0]
                key1 = i.split(',')[1]
            }
            if (typeof item[key] !== 'undefined') {
                newItem[mapObj[i]] = key1
                    ? item[key] + '(' + (item[key1] ? item[key1] : '') + ')'
                    : item[key]
                delete newItem[key]
            }
        }

        newData.push(newItem)
    })
    return newData
}
/**
 * 判断两个对象或者数组是否相等
 * @param obj1
 * @param obj2
 * @returns {boolean}
 */
export function isEqual (obj1, obj2) {
    return JSON.stringify(obj1) === JSON.stringify(obj2)
}

/**
 * 比较函数
 * @param {Object} param1 要比较的参数1
 * @param {Object} param2 要比较的参数2
 * @return {Number} 如果param1 > param2 返回 1
 *                     如果param1 == param2 返回 0
 *                     如果param1 < param2 返回 -1
 */
function compareFunc (param1, param2) {
    //如果两个参数均为字符串类型
    if (typeof param1 == 'string' && typeof param2 == 'string') {
        return param1.localeCompare(param2)
    }
    //如果参数1为数字，参数2为字符串
    if (typeof param1 == 'number' && typeof param2 == 'string') {
        return -1
    }
    //如果参数1为字符串，参数2为数字
    if (typeof param1 == 'string' && typeof param2 == 'number') {
        return 1
    }
    //如果两个参数均为数字
    if (typeof param1 == 'number' && typeof param2 == 'number') {
        if (param1 > param2) {
            return 1
        }
        if (param1 == param2) {
            return 0
        }
        if (param1 < param2) {
            return -1
        }
    }
}
export const isEmptyObj = arr => {
    let flag = true
    for (let key in arr) {
        if (arr[key] && arr[key] != '') {
            flag = false
            break
        }
    }

    return flag
}